﻿using BCCommon;
using BCCommon.Enums;
using BCEntity.MongoDB.PushMessage;
using BCService.Common.PushMessage.PushMessageSend.Driver;
using BCService.Common.PushMessage.PushMessageSend.Order;
using BCService.Kafka;
using BCService.PushMessage;
using Newtonsoft.Json;
using System;
using System.Threading.Tasks;

namespace BCService.Common.PushMessage.PushMessageSend
{
    public class PushMessageSendService : IPushMessageSendService
    {
        private readonly IPushMessageForOrderService pushMessageForOrderService;
        private readonly IPushMessageForDriverService pushMessageForDriverService;
        private readonly IKafkaProducerService kafkaProducerService;
        private readonly IAliyunPushService aliyunPushService;

        public PushMessageSendService(IPushMessageForOrderService pushMessageForOrderService, 
            IPushMessageForDriverService pushMessageForDriverService,
            IKafkaProducerService kafkaProducerService,
            IAliyunPushService aliyunPushService)
        {
            this.pushMessageForOrderService = pushMessageForOrderService;
            this.pushMessageForDriverService = pushMessageForDriverService;
            this.kafkaProducerService = kafkaProducerService;
            this.aliyunPushService = aliyunPushService;
        }

        public async Task PushMessageForDriverTrain(long[] userIds, PushMessageType pushMessageType, DateTime start, DateTime end)
        {
            await Task.Run(() =>
            {
                PushMessageMongoEntity pushMessageMongoEntity = this.pushMessageForDriverService.GetPushMessageMongoEntity(userIds,
                    pushMessageType, start, end);
                Push(pushMessageMongoEntity);
            });
        }
        public async Task PushMessageForOrder(long orderId, PushMessageType pushMessageType)
        {
            await Task.Run(() =>
            {
                PushMessageMongoEntity pushMessageMongoEntity = this.pushMessageForOrderService.GetPushMessageMongoEntity(orderId, pushMessageType);
                Push(pushMessageMongoEntity);
            });
        }
        public async Task PushMessageForDriverAppVehicleCancelPassed(long userId, string orderNo, PushMessageType pushMessageType)
        {
            await Task.Run(() =>
            {
                PushMessageMongoEntity pushMessageMongoEntity = this.pushMessageForDriverService.GetPushMessageMongoEntity(userId, pushMessageType, orderNo);

                Push(pushMessageMongoEntity);
            });
        }

        private void Push(PushMessageMongoEntity pushMessageMongoEntity)
        {
            if (pushMessageMongoEntity != null)
            {
                this.aliyunPushService.UnicastAsync(
                   pushMessageMongoEntity.AppType,
                   pushMessageMongoEntity.UserIds,
                   pushMessageMongoEntity.Title,
                   pushMessageMongoEntity.Body,
                   JsonConvert.DeserializeObject(pushMessageMongoEntity.Obj),
                   pushMessageMongoEntity.BarTypeId,
                   pushMessageMongoEntity.IosMusic);
                this.kafkaProducerService.SendPushMessage(pushMessageMongoEntity);
            }
        }
    }
}
